package com.ty.zls.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.qmuiteam.qmui.widget.QMUIEmptyView;
import com.qmuiteam.qmui.widget.QMUITabSegment;
import com.ty.zls.MainActivity;
import com.ty.zls.R;
import com.ty.zls.adapter.HadWaterAdapter;
import com.ty.zls.adapter.NeedWaterAdapter;
import com.ty.zls.bean.Account;
import com.ty.zls.components.MyReceycleView;
import com.ty.zls.listener.HadWaterItemListener;
import com.ty.zls.listener.HadWaterLoadMoreListener;
import com.ty.zls.listener.HadWaterRefreshListener;
import com.ty.zls.listener.NeedWaterItemListener;
import com.ty.zls.listener.NeedWaterLoadMoreListener;
import com.ty.zls.listener.NeedWaterRefreshListener;
import com.ty.zls.widget.MySearchView;

import java.util.ArrayList;
import java.util.List;


public class NeedWaterFragment extends RelativeLayout {

    private Context context;
    private QMUITabSegment.Tab needWater;
    private boolean initFlag = false;
    NeedWaterRefreshListener needWaterRefreshListener;
    private List<Account> mListsSearch = new ArrayList<>();
    private List<Account> accounts;


    public NeedWaterFragment(Context context, QMUITabSegment.Tab needWater) {
        super(context);
        this.context = context;
        this.needWater = needWater;
        initView();
    }

    public void initView() {
        //未抄用户
        View view = LayoutInflater.from(context).inflate(R.layout.need_water_fragment, this);
        final SwipeToLoadLayout swipeToLoadLayout = view.findViewById(R.id.swipeToLoadLayout);
        //设配器
        MyReceycleView listView = view.findViewById(R.id.swipe_target);
        MySearchView mySearchView = view.findViewById(R.id.my_search_view);
        listView.setEmptyView(view.findViewById(R.id.needwater_empty));
        listView.setLayout(swipeToLoadLayout);

        accounts = new ArrayList<>();
        final NeedWaterAdapter adapter = new NeedWaterAdapter(accounts, context);
        listView.setLayoutManager(new LinearLayoutManager(context));
        listView.setAdapter(adapter);

        needWaterRefreshListener = new NeedWaterRefreshListener(adapter, context, swipeToLoadLayout, (QMUIEmptyView) listView.getmEmptyView());
        swipeToLoadLayout.setOnRefreshListener(needWaterRefreshListener);
        swipeToLoadLayout.setOnLoadMoreListener(new NeedWaterLoadMoreListener(adapter, context, swipeToLoadLayout, (QMUIEmptyView) listView.getmEmptyView()));
        adapter.setOnItemClickListener(new NeedWaterItemListener(context, adapter));
        adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                needWater.showSignCountView(context, adapter.getTotal());
            }
        });

        mySearchView.setOnComplete(new MySearchView.onComplete() {
            @Override
            public void onCompleteInput(String str) {
                /*List<Account> datas = adapter.getDatas();
                for (int i = 0; i < datas.size(); i++) {
                    if (str.equals(datas.get(i).getTel())) {
                        Log.e("aaaaaa", "onCompleteInput: " + datas.get(i).getName());
                    }
                }*/
            }

            @Override
            public void onQueryTextChangeStr(String str) {
                mListsSearch.clear();
                if (TextUtils.isEmpty(str)){
                    adapter.setDatas(accounts);
                    adapter.notifyDataSetChanged();
                    return;
                }
                for (int i = 0; i < accounts.size(); i++) {
                    if (accounts.get(i).getTel() != null && accounts.get(i).getTel().startsWith(str)||
                            accounts.get(i).getName().startsWith(str)||
                            accounts.get(i).getId().startsWith(str)||
                            accounts.get(i).getSort().startsWith(str)) {
                        Log.e("aaaaaa", "onQueryTextChangeStr: " + accounts.get(i).getName());
                        mListsSearch.add(accounts.get(i));
                    }
                }
                adapter.setDatas(mListsSearch);
                adapter.notifyDataSetChanged();
            }
        });
    }

    public void init() {
        if (!initFlag) {
            needWaterRefreshListener.autoRefresh();
            initFlag = true;
        }
    }

}
